package com.approval.approvalapp.center.tz;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.JsdxListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.Holder> {

    private List<JsdxListBean> list;
    private LeftClick leftClick;

    public void setList(List<JsdxListBean> list,LeftClick leftClick) {
        this.leftClick = leftClick;
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_left, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        if (list.get(position).isSelect()) {
            holder.root.setBackgroundColor(Color.parseColor("#ffffff"));
            holder.tv.setTextColor(Color.parseColor("#1076FF"));
        } else {
            holder.root.setBackgroundColor(Color.parseColor("#f5f5f5"));
            holder.tv.setTextColor(Color.parseColor("#333333"));
        }

        holder.tv.setText(list.get(position).getOrgname());

        JsdxListBean jsdxListBean = list.get(position);
        List<JsdxListBean.ListBean> list = jsdxListBean.getList();
        List<String> list1 = new ArrayList<>();
        list1.clear();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isSelect()) {
                list1.add("1");
            }
        }

        if (list1.size() > 0) {
            holder.redRl.setVisibility(View.VISIBLE);
            holder.redTv.setText(list1.size() + "");
        } else {
            holder.redRl.setVisibility(View.GONE);
        }

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                leftClick.leftclick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv)
        TextView tv;
        @Bind(R.id.red_tv)
        TextView redTv;
        @Bind(R.id.red_rl)
        RelativeLayout redRl;
        @Bind(R.id.root)
        RelativeLayout root;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface LeftClick{
        public void leftclick(int position);
    }

}
